package www.openteam.com.view.policy;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.chaychan.library.UIUtils;
import com.nineoldandroids.animation.ObjectAnimator;

import www.openteam.com.R;

/**
 * Created by Administrator on 2019-07-01.
 */

public class MEXpandableLinearLayout extends LinearLayout implements View.OnClickListener {
    private static final String TAG = MEXpandableLinearLayout.class.getSimpleName();
    private TextView tvTip;
    private ImageView ivArrow;
    private boolean isExpand;
    private int defaultItemCount;
    private String expandText;
    private String hideText;
    private boolean useDefaultBottom;
    public boolean hasBottom;
    private View bottomView;
    private float fontSize;
    private int textColor;
    private int arrowResId;
    private int mPosition;
    private OnStateChangeListener mStateListener;

    public MEXpandableLinearLayout(Context context) {
        super(context);
        Log.i(TAG, "MEXpandableLinearLayout: 1");
    }

    public MEXpandableLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs,0);
        this.isExpand = false;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MExpandableLinearLayout);
        this.defaultItemCount = ta.getInt(R.styleable.MExpandableLinearLayout_defaultItemCount, 2);
        this.expandText = ta.getString(R.styleable.MExpandableLinearLayout_expandText);
        this.hideText = ta.getString(R.styleable.MExpandableLinearLayout_hideText);
        this.fontSize = ta.getDimension(R.styleable.MExpandableLinearLayout_tipTextSize, (float) UIUtils.sp2px(context, 14.0F));
        this.textColor = ta.getColor(R.styleable.MExpandableLinearLayout_tipTextColor, Color.parseColor("#666666"));
        this.arrowResId = ta.getResourceId(R.styleable.MExpandableLinearLayout_arrowDownImg, com.chaychan.library.R.mipmap.arrow_down);
        this.useDefaultBottom = ta.getBoolean(R.styleable.MExpandableLinearLayout_useDefaultBottom, true);
        Log.e(TAG, "defaultItemCount = " + defaultItemCount + " , hideText = " + hideText);
        ta.recycle();
        this.setOrientation(LinearLayout.VERTICAL);
    }

    public MEXpandableLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.isExpand = false;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MExpandableLinearLayout);
        this.defaultItemCount = ta.getInt(R.styleable.MExpandableLinearLayout_defaultItemCount, 2);
        this.expandText = ta.getString(R.styleable.MExpandableLinearLayout_expandText);
        this.hideText = ta.getString(R.styleable.MExpandableLinearLayout_hideText);
        this.fontSize = ta.getDimension(R.styleable.MExpandableLinearLayout_tipTextSize, (float) UIUtils.sp2px(context, 14.0F));
        this.textColor = ta.getColor(R.styleable.MExpandableLinearLayout_tipTextColor, Color.parseColor("#666666"));
        this.arrowResId = ta.getResourceId(R.styleable.MExpandableLinearLayout_arrowDownImg, com.chaychan.library.R.mipmap.arrow_down);
        this.useDefaultBottom = ta.getBoolean(R.styleable.MExpandableLinearLayout_useDefaultBottom, true);
        Log.e(TAG, "defaultItemCount = " + defaultItemCount + " , hideText = " + hideText);
        ta.recycle();
        this.setOrientation(LinearLayout.VERTICAL);
    }


    protected void onFinishInflate() {
        super.onFinishInflate();
        this.findViews();
    }

    private void findViews() {
        this.bottomView = View.inflate(this.getContext(), R.layout.item_ell_bottom, (ViewGroup) null);
        this.ivArrow = (ImageView) this.bottomView.findViewById(R.id.iv_arrow);
        this.tvTip = (TextView) this.bottomView.findViewById(R.id.tv_tip);
        this.tvTip.getPaint().setTextSize(this.fontSize);
        this.tvTip.setTextColor(this.textColor);
        this.ivArrow.setImageResource(this.arrowResId);
        this.bottomView.setOnClickListener(this);
    }

    public void addItem(View view) {
        int childCount;
        if (!this.useDefaultBottom) {
            this.addView(view);
            childCount = this.getChildCount();
            if (childCount > this.defaultItemCount) {
                this.hide();
            }

        } else {
            if (!this.hasBottom) {
                this.addView(view);
            } else {
                childCount = this.getChildCount();
                this.addView(view, childCount - 1);
            }

            this.refreshUI(view);
        }
    }

    public void setOrientation(int orientation) {
        if (0 == orientation) {
            throw new IllegalArgumentException("ExpandableTextView only supports Vertical Orientation.");
        } else {
            super.setOrientation(orientation);
        }
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = this.getChildCount();
        Log.i(TAG, "childCount: " + childCount);
        this.justToAddBottom(childCount);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private void justToAddBottom(int childCount) {
//        Log.i(TAG, "childCount: " + childCount);
//        Log.i(TAG, "defaultItemCount: " + defaultItemCount);
//        Log.i(TAG, "useDefaultBottom: " + useDefaultBottom);
//        Log.i(TAG, "hasBottom: " + hasBottom);
        if (childCount > this.defaultItemCount && this.useDefaultBottom && !this.hasBottom) {
            this.addView(this.bottomView);
            this.hide();
            this.hasBottom = true;
        }

    }

    private void refreshUI(View view) {
        int childCount = this.getChildCount();
        if (childCount > this.defaultItemCount) {
            if (childCount - this.defaultItemCount == 1) {
                this.justToAddBottom(childCount);
            }

            view.setVisibility(GONE);
        }

    }

    public void expand() {

//        Log.i(TAG, "defaultItemCount: " + defaultItemCount);
//        for (int i = this.defaultItemCount; i < this.getChildCount(); ++i) {
//            View view = this.getChildAt(i);
//            view.setVisibility(VISIBLE);
//        }

        for (int i = 0; i < this.getChildCount()-defaultItemCount; ++i) {
            View view = this.getChildAt(i);
            view.setVisibility(VISIBLE);
        }

    }

    public void hide() {
        int endIndex = this.useDefaultBottom ? this.getChildCount() - 1 : this.getChildCount();
//        Log.i(TAG, "defaultItemCount: " + defaultItemCount);
//        for (int i = this.defaultItemCount; i < endIndex; ++i) {
//            View view = this.getChildAt(i);
//            view.setVisibility(GONE);
//        }

        for (int i = 0; i < endIndex-defaultItemCount; i++) {
            View view = this.getChildAt(i);
            view.setVisibility(GONE);
        }

    }

    private void doArrowAnim() {
        if (this.isExpand) {
            ObjectAnimator.ofFloat(this.ivArrow, "rotation", new float[]{-180.0F, 0.0F}).start();
        } else {
            ObjectAnimator.ofFloat(this.ivArrow, "rotation", new float[]{0.0F, 180.0F}).start();
        }

    }

    public void onClick(View v) {
        this.toggle();
    }

    public void toggle() {
        if (this.isExpand) {
            this.hide();
            this.tvTip.setText(this.expandText);
        } else {
            this.expand();
            this.tvTip.setText(this.hideText);
        }

        this.doArrowAnim();
        this.isExpand = !this.isExpand;
        if (this.mStateListener != null) {
            this.mStateListener.onStateChanged(this.isExpand);
        }

    }

    public void registerViewObserver (View view){
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                toggle();
            }
        });
    }

    public void setOnStateChangeListener(OnStateChangeListener mListener) {
        this.mStateListener = mListener;
    }

    public void setOnItemClickListener(final OnItemClickListener listener) {
        int endIndex = this.useDefaultBottom ? this.getChildCount() - 1 : this.getChildCount();

        for (int i = 0; i < endIndex; ++i) {
            View view = this.getChildAt(i);
            final int fianli = i;
            view.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    listener.onItemClick(v, fianli);
                }
            });
        }

    }

    public interface OnItemClickListener {
        void onItemClick(View var1, int var2);
    }

    public interface OnStateChangeListener {
        void onStateChanged(boolean var1);
    }

}
